﻿using System;
using OpenGiving;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.Profile;
using System.Linq;

public partial class UserRegistration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated) Response.Redirect("~/Default.aspx");

        (Master.FindControl("PageSubtitleLabel") as Label).Text = "Create an account here";
    }

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        ProfileCommon newProfile = ProfileBase.Create(CreateUserWizard1.UserName) as ProfileCommon;

        if (newProfile == null)
        {
            ErrorLabel.Text = "Failure retrieving user profile";
            return;
        }

        using (OpenGivingDataContext db = new OpenGivingDataContext())
        {
            try
            {
                MembershipUser u = Membership.GetUser(CreateUserWizard1.UserName);

                UserMappings map = new UserMappings();
                map.userID = (Guid)u.ProviderUserKey;
                // TODO: Not good to have data in both places...
                map.FirstName = txtFName.Text;
                map.LastName = txtLName.Text;

                db.UserMappings.InsertOnSubmit(map);

                db.SubmitChanges();
                
                newProfile.OpenGivingID = map.contact_id;

                // Save contact details
                newProfile.FirstName = txtFName.Text;
                newProfile.LastName = txtLName.Text;
                newProfile.Street1 = txtAddr1.Text;
                //newProfile.Street2 = GetTextBoxText(CreateUserWizard1.WizardSteps[1], "txtAddr2");
                //newProfile.Street3 = GetTextBoxText(CreateUserWizard1.WizardSteps[1], "txtAddr3");
                newProfile.MainPhone = txtMainPhone.Text;
                newProfile.City = txtCity.Text;
                newProfile.State = ddlState.Text;
                newProfile.PostalCode = txtPostalCode.Text;
                newProfile.Employer = txtEmployer.Text;
                newProfile.Gender = ddlGender.Text;
                newProfile.GroupAffiliation = ddlGroup.Text;
                
                // Save skills
                newProfile.OpenGivingSkills = UserSkillsUserControl1.GetSkillSet();

                // TODO: THIS IS FOR TESTING.  REMOVE SOON!!!
                if (cbAdmin.Checked) Roles.AddUserToRole(u.UserName, "Admin");
                else Roles.AddUserToRole(u.UserName, "Volunteer");

                newProfile.Save();
                
                Response.Redirect("UserProfile.aspx");
            }
            catch (Exception x)
            {
                // Somehow log the exception
                ErrorLabel.Text = string.Format("<p>{0}</p>", x.Message);
            }

        }
    }

    private string GetTextBoxText(Control parent, string fieldName)
    {
        TextBox tb = CreateUserWizard1.WizardSteps[1].FindControl(fieldName) as TextBox;
        return tb.Text;
    }

}
